//
//  SFPCheckAppointmentController.swift
//  StriveForPregnancy
//
//  Created by iOS-Czz on 2023/11/9.
//

import UIKit

class SFPCheckAppointmentController: SFPBaseViewController {
    @IBOutlet var tableView: UITableView!
    @IBOutlet var progressView: CircleProgressView!
    @IBOutlet var totalLabel: UILabel!
    
    lazy var viewModel: SFPCheckAppointmentViewModel = {
        return SFPCheckAppointmentViewModel()
    }() 
   
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        loadData()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
       setTableView()
        
        // Do any additional setup after loading the view.
    }
    func setTableView(){
        self.tableView.dataSource = self
        self.tableView.delegate = self
        self.tableView.register(UINib(nibName: "SFPCheckAppointmentCell", bundle: .main), forCellReuseIdentifier: "SFPCheckAppointmentCell")
    }
    @IBAction func gotoAddCheckAppointment(_ sender: Any) {
        self.navigationController?.pushViewController(SFPAddCheckAppointmentController(), animated: true)
    }
    func loadData(){
        self.viewModel.getPageData() { models in
            self.progressView.progress =  self.viewModel.progress
            let modifiedAttributedString = NSAttributedString.modifySubstring(self.viewModel.totalInfo, targetSubstring: self.viewModel.totalInfo.components(separatedBy: "/").first!, color: .black, font: UIFont.systemFont(ofSize: 50, weight: .bold))
            self.totalLabel.attributedText = modifiedAttributedString
            self.tableView.reloadData()
        }
        
    }
}
extension SFPCheckAppointmentController:UITableViewDataSource,UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.viewModel.dataSource.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:SFPCheckAppointmentCell = tableView.dequeueReusableCell(withIdentifier: "SFPCheckAppointmentCell") as! SFPCheckAppointmentCell
        cell.model = self.viewModel.dataSource[indexPath.row]
        cell.refreshBlock = { [self] in
            self.loadData()
        }
        return cell
    }
}
